package com.jk11a.shangdaast.library;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.LibraryDao;
import com.jk11a.shangdaast.dao.UserDao;
import com.jk11a.shangdaast.user.LoginActivity;
import com.jk11a.shangdaast.user.User;

public class lib_user extends Activity{
	
	private SharedPreferences sp=null;
	private String FILE = "saveUserNamePwd";
	private Thread getreaderThread;
	 private static final int MSG_SUCCESS = 0; //获取成功
	 private static final int MSG_FAILURE = 1; //获取失败
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lib_user);   
		sp=this.getSharedPreferences(FILE, 0);
		if(getreaderThread==null){
			getreaderThread=new Thread(runnable);
			getreaderThread.start();
		}
		else {
			Toast.makeText(getApplication(), "getuserThread thread 已启动",  Toast.LENGTH_LONG).show();
		}
    }
	Runnable runnable=new Runnable() {
		// 重写run()方法，此方法在新的线程中运行
		@Override
		public void run() {
			// TODO Auto-generated method stub
			LibraryDao libraryDao=new LibraryDao();
			Reader reader=libraryDao.getReader(sp.getInt("id", 0));
			// 获取 成功，向UI线程发送MSG_SUCCESS标识和books对象
			if(reader!=null){
			mHandler.obtainMessage(MSG_SUCCESS,reader).sendToTarget();
			}
			else{
				// 获取 失败，向UI线程发送MSG_FAILURE标识
				 mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				 return;
			}
		}
	};
	private Handler mHandler=new Handler(){
		// 重写handleMessage()方法，此方法在UI线程运行
		@Override
		public void handleMessage(Message msg){
			switch (msg.what) {
			case MSG_SUCCESS:
				Log.i("msg obj","userinfo success");
				Reader reader=(Reader)msg.obj;
				Log.i("msg obj",reader.getName()+"!!");
				showinfo(reader);
				break;

			case MSG_FAILURE:
				Log.i("msg obj","failed");
				Toast.makeText(getApplication(), "获取用户信息失败",  Toast.LENGTH_LONG).show();
				break;
			}
		}
	};
	protected void jmp_history(View v){
		Intent intent=new Intent();
		  intent.setClass(lib_user.this,lib_history.class);
		  startActivity(intent);
	}
	

	
	protected void exit_lib(View v){
		finish();
		
	}
	
	public void showinfo(Reader reader){
		TextView tv_name = new TextView(this);
		TextView tv_cardid=new TextView(this);
		TextView tv_orderbook = new TextView(this);
		TextView tv_overingbook = new TextView(this);
		TextView tv_overedbook= new TextView(this);
		TextView tv_totalbook= new TextView(this);
		TextView tv_weituobook= new TextView(this);
		tv_name.setText("用户姓名：  "+reader.getName());
		tv_name.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_name.setTextColor(Color.rgb(0, 0, 0));
		tv_cardid.setText("学生卡号:"+reader.getCardid());
		tv_cardid.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_cardid.setTextColor(Color.rgb(0, 0, 0));
		tv_overingbook.setText("即将超期图书  "+reader.getOveringbook());
		tv_overingbook.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_overingbook.setTextColor(Color.rgb(0, 0, 0));
		tv_totalbook.setText("共借阅图书："+reader.getTotalbook());
		tv_totalbook.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_totalbook.setTextColor(Color.rgb(0, 0, 0));
		tv_orderbook.setText("已预约图书："+reader.getOrderbook());
		tv_orderbook.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_orderbook.setTextColor(Color.rgb(0, 0, 0));
		tv_overedbook.setText("已超期图书： "+reader.getOveredbook());
		tv_overedbook.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_overedbook.setTextColor(Color.rgb(0, 0, 0));
		tv_weituobook.setText("已委托图书： "+reader.getWeituobook());
		tv_weituobook.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_weituobook.setTextColor(Color.rgb(0, 0, 0));
		addlayout_top(tv_name);
		addlayout(tv_cardid);
		addlayout(tv_overingbook);
		addlayout(tv_overedbook);
		addlayout(tv_orderbook);
		addlayout(tv_weituobook);
		addlayout(tv_totalbook);
}
public void addlayout_top(TextView tv){
	RelativeLayout rLayout = new RelativeLayout(this);	
	rLayout.setBackgroundResource(R.drawable.preference_first_item);
	rLayout.setTop(14);
	rLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
	rLayout.setGravity(Gravity.CENTER);
	rLayout.addView(tv);   
	((LinearLayout) this.findViewById(R.id.readerinfo_lilayout)).addView(rLayout);
}
public void addlayout(TextView tv){
	RelativeLayout rLayout = new RelativeLayout(this);	
	rLayout.setBackgroundResource(R.drawable.preference_item);
	rLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
	rLayout.setGravity(Gravity.CENTER);
	rLayout.addView(tv);   
	((LinearLayout) this.findViewById(R.id.readerinfo_lilayout)).addView(rLayout);
}
	
}
